package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YouNakangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "younakang")
public class YouNaKangAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(YouNaKangAdaptor.class);
	private static String HOST_CALLBACK_URL = BetweenProperties.CALLBACK_URL;//回调地址
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
	
		String productid = orderBean.getProductCode();
		String mobile = orderBean.getPhoneNo();
		String sporderid = orderBean.getIds();
		String price = orderBean.getProductPrice().toString();
		String back_url = HOST_CALLBACK_URL+"Younak";
		log.info("between- 尤纳康订单YouNaKangService.order() 订购请求productid:" + productid+",mobile:"+mobile+",sporderid:"+sporderid+",price"+price+",back_url:"+back_url);
		String result = YouNakangServiceImpl.service.order(productid, mobile, sporderid,price,back_url);
		log.info("between- 尤纳康订单YouNaKangService.order() 订购响应response<>:" + result);
			
	
		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("resultno");
			

		if ("0".equals(code)||"1".equals(code)||"2".equals(code)) {
			orderBean.setMessage("订购中");
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
		} else {
			orderBean.setMessage("订购失败");
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
			
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	
	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
	
		String cstmOrderNo = orderBean.getIds();
		log.info("between -尤纳康状态YouNaKangService.orderStatus() 查询请求cstmOrderNo:" +cstmOrderNo);

		String result = YouNakangServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 尤纳康状态YouNaKangService.orderStatus() 查询响应response<> :" + result);
	
		
        JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("resultno");

		if ("1".equals(code)) {
		  orderBean.setMessage("订购成功");
		  orderBean.setErrorMsg("订购成功");
		  orderBean.setStatus("7");
		} else if ("0".equals(code)||"2".equals(code)) {
		  orderBean.setMessage("订购中");
		  orderBean.setErrorMsg("订购中");
		  orderBean.setStatus("1");
		} else {
		  orderBean.setMessage("订购失败");
		  orderBean.setErrorMsg("订购失败");
		  orderBean.setStatus("8");
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
